import { Component, OnInit, ViewChild } from '@angular/core';
import { SystemFileDTO } from 'src/app/project/model/icm/icm-global/system-file/system-file-dto';
import { AttachSmallComponent } from '@shared/common-component/attach-small.component';
import { SettingsService } from '@delon/theme';
import { StudentService } from 'src/app/project/service/icm/icm-system-user/student/student.service';
import { StudentCreditRecordDTO } from 'src/app/project/model/icm/icm-student-status/academic-progress/student-credit-record.dto';
import { StudentDTO } from 'src/app/project/model/icm/icm-system-user/student/student.dto';
import { StudentCreditRecordService } from 'src/app/project/service/icm/icm-student-status/academic-progress/student-credit-record.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-academic-progress-view',
  templateUrl: './academic-progress-view.component.html',
  styles: [],
})
export class AcademicProgressViewComponent implements OnInit {
  constructor(
    private studentService: StudentService,
    private settingService: SettingsService,
    private studentCreditRecordService: StudentCreditRecordService,
    private msg: NzMessageService,
  ) {}

  /**
   * 附件
   */
  @ViewChild('attachSmallComponent', { static: false })
  attachSmallComponent: AttachSmallComponent;

  /**
   * 当前登录学生
   */
  student: StudentDTO;
  studentId = null;

  /**
   * 学生学业学分记录
   */
  studentCreditRecord: StudentCreditRecordDTO = {
    id: null,
    tag: null,
    academicProgress: {
      id: null,
      compulsoryCredit: null,
      subjectBasedElective: null,
      professionElective: null,
      publicElective: null,
      practiceCredit: null,
      innovationScore: 0,
      graduationInternship: null,
      graduationThesis: null,
      total: null,
    },
    academyName: null,
    className: null,
    studentCode: null,
    studentName: null,
    gender: null,
    grade: null,
    compulsoryCredit: null,
    subjectBasedElective: null,
    professionElective: null,
    publicElective: null,
    practiceCredit: null,
    innovationScore: 0,
    graduationInternship: null,
    graduationThesis: null,
    total: null,
    files: null,
    fileIds: [],
  };

  /**
   * 主题
   */
  theme = 'macarons';

  /**
   * 图1
   */
  optionsAddress: any;

  /**
   * 附件是否只读
   */
  isReview: boolean;

  /**
   * 附件组件按钮可见性
   */
  isShow: boolean;

  /**
   * 文件类型
   */
  fileType: string;

  /**
   * 附件数据
   */
  attachFiles: SystemFileDTO[];

  /**
   * 是否预览
   */
  isPreView = false;

  /**
   * table数据
   */
  listOfData: Array<{ type: string; creditsTaken: number; targetCredits: number }> = [];

  /**
   * 刷新
   */
  reflesh() {
    this.studentId = this.settingService.user.id;
    this.studentService.findById(this.studentId).subscribe(data => {
      this.student = data;
      setTimeout(() => {
        // tslint:disable-next-line: no-shadowed-variable
        this.studentCreditRecordService.findByStudentCode(this.student.code).subscribe(data => {
          this.studentCreditRecord = data;
          this.listOfData = [];
          this.listOfData.push({
            type: '必修课',
            creditsTaken: this.studentCreditRecord.compulsoryCredit,
            targetCredits: this.studentCreditRecord.academicProgress.compulsoryCredit,
          });
          this.listOfData.push({
            type: '学科基础选修课',
            creditsTaken: this.studentCreditRecord.subjectBasedElective,
            targetCredits: this.studentCreditRecord.academicProgress.subjectBasedElective,
          });
          this.listOfData.push({
            type: '专业选修课',
            creditsTaken: this.studentCreditRecord.professionElective,
            targetCredits: this.studentCreditRecord.academicProgress.professionElective,
          });
          this.listOfData.push({
            type: '公共选修课',
            creditsTaken: this.studentCreditRecord.publicElective,
            targetCredits: this.studentCreditRecord.academicProgress.publicElective,
          });
          this.listOfData.push({
            type: '实践学分',
            creditsTaken: this.studentCreditRecord.practiceCredit,
            targetCredits: this.studentCreditRecord.academicProgress.practiceCredit,
          });
          this.listOfData.push({
            type: '创新分',
            creditsTaken: this.studentCreditRecord.innovationScore,
            targetCredits: this.studentCreditRecord.academicProgress.innovationScore,
          });
          this.listOfData.push({
            type: '毕业实习',
            creditsTaken: this.studentCreditRecord.graduationInternship,
            targetCredits: this.studentCreditRecord.academicProgress.graduationInternship,
          });
          this.listOfData.push({
            type: '毕业论文',
            creditsTaken: this.studentCreditRecord.graduationThesis,
            targetCredits: this.studentCreditRecord.academicProgress.graduationThesis,
          });
          this.listOfData.push({
            type: '合计',
            creditsTaken: this.studentCreditRecord.total,
            targetCredits: this.studentCreditRecord.academicProgress.total,
          });

          this.optionsAddress = {
            title: {
              text: '各项学分修读情况',
            },
            tooltip: {},
            legend: {
              data: ['销量'],
            },
            xAxis: {
              data: [
                '必修课',
                '学科基础选修课',
                '专业选修课',
                '公共选修课',
                '实践学分',
                '创新分',
                '毕业实习',
                '毕业论文',
              ],
            },
            yAxis: {
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false,
              },
            },
            series: [
              {
                name: '学分',
                type: 'bar',
                data: [
                  this.studentCreditRecord.compulsoryCredit,
                  this.studentCreditRecord.subjectBasedElective,
                  this.studentCreditRecord.professionElective,
                  this.studentCreditRecord.publicElective,
                  this.studentCreditRecord.practiceCredit,
                  this.studentCreditRecord.innovationScore,
                  this.studentCreditRecord.graduationInternship,
                  this.studentCreditRecord.graduationThesis,
                ],
              },
            ],
          };
        });
      }, 500);
    });
  }

  /**
   * 上传附件
   */
  uploadAttach(fileType: string, files?: SystemFileDTO[]) {
    this.fileType = fileType;
    this.attachFiles = files;
    this.attachSmallComponent.isVisible = true;
    this.isReview = this.isPreView;
    this.isShow = !this.isPreView;
  }

  /**
   * 上传材料
   */
  upload() {
    this.isPreView = false;
    this.uploadAttach('创新分加分材料', this.studentCreditRecord.files);
  }

  /**
   * 查看
   */
  onClick() {
    this.isPreView = true;
    this.uploadAttach('创新分加分材料', this.studentCreditRecord.files);
  }

  /**
   * 将附件弹射回detail
   */
  resultChange(files: SystemFileDTO[]) {
    this.studentCreditRecord.files = files;
    this.studentCreditRecord.fileIds = [];
    this.studentCreditRecord.files.forEach(file => {
      this.studentCreditRecord.fileIds.push(file.id);
    });
    // 提交保存，并提示
    this.complete();
  }

  /**
   * 提交数据
   */
  complete() {
    this.studentCreditRecord.fileIds = [];
    this.studentCreditRecord.files.forEach(data => {
      this.studentCreditRecord.fileIds.push(data.id);
    });
    this.studentCreditRecordService.complete(this.studentCreditRecord.id, this.studentCreditRecord).subscribe(
      data => {
        this.studentCreditRecord = data;
        this.reflesh();
      },
      () => this.msg.error('保存失败,请重试'),
      () => this.msg.success('保存成功,正在刷新页面'),
    );
  }

  ngOnInit() {
    this.reflesh();
  }
}
